/*
 * @Author: szx
 * @Date: 2021-11-23 12:52:51
 * @LastEditTime: 2021-11-23 13:13:28
 * @Description:
 * @FilePath: \leetcode\800-899\859\859.js
 */
/**
 * @param {string} s
 * @param {string} goal
 * @return {boolean}
 */
var buddyStrings = function (s, goal) {
    if (s.length != goal.length) return false;
    const n = s.length;
    const map = new Map();
    let diff = [];
    if (s == goal) {
        for (let i of s) {
            if (map.get(i) == 1) {
                return true;
            } else {
                map.set(i, 1);
            }
        }
    }
    for (let i = 0; i < n; i++) {
        if (s[i] != goal[i]) {
            diff.push(s[i], goal[i]);
        }
    }
    if (diff.length == 4 && diff[0] == diff[3] && diff[1] == diff[2]) {
        return true;
    }
    return false;
};
console.log(buddyStrings('ab', 'ba'));
